package com.intel.two.service.user.Impl;


import com.intel.two.mapper.UserMapper;
import com.intel.two.pojo.user.User;
import com.intel.two.service.user.UserService;
import com.intel.two.utils.StringUtil;
import com.intel.two.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @Author: Zz
 * @Description: TODO
 * @DateTime: 2022/10/13 14:13
 **/
/*
 *                        .::::.
 *                      .::::::::.
 *                     :::::::::::
 *                  ..:::::::::::'
 *               '::::::::::::'
 *                 .::::::::::
 *            '::::::::::::::..
 *                 ..::::::::::::.
 *               ``::::::::::::::::
 *                ::::``:::::::::'        .:::.
 *               ::::'   ':::::'       .::::::::.
 *             .::::'      ::::     .:::::::'::::.
 *            .:::'       :::::  .:::::::::' ':::::.
 *           .::'        :::::.:::::::::'      ':::::.
 *          .::'         ::::::::::::::'         ``::::.
 *      ...:::           ::::::::::::'              ``::.
 *     ````':.          ':::::::::'                  ::::..
 *                        '.:::::'                    ':'````..
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;


    @Override
    public Result save(User user) {
        if(mapper.save(user)>0){
            return Result.ok("注册成功");
        }else {
            return Result.fail("注册失败");
        }
    }

    @Override
    public Result update(User user) {
        if (mapper.update(user)>0){
            return Result.ok("修改成功");
        }else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result login(User user, HttpSession session) {
        //1.校验参数
        if (StringUtil.isNotEmpty(user.getAccount(),user.getPassword())){
            //2.查询数据库
            User user1 = mapper.selectOne(user);
            //3.验证用户是否存在
            if (user1 != null){
                //4.验证账号的有效性
                if (user1.getFlag() == 1){
                    //5.验证密码是否正确
                    if (user1.getPassword().equals(user.getPassword())){
                        //6.登录成功 储存
                        session.setAttribute("user",user1);
                        session.setMaxInactiveInterval(60*60*24);
                        return Result.ok();
                    }
                }
            }
        }
        //7.防止撞库
        return Result.fail("账号或密码不正确");
    }
}
